/**
 * @class Spark.DateTimePicker
 * @extends Spark.DatePicker
 * <p>DateTimePicker组件.
 * @author njq.niu@hand-china.com
 * @constructor
 * @param {Object} config 配置对象. 
 */
$S.DateTimePicker = Ext.extend($S.DatePicker,{
    constructor: function(config) {
        $S.DateTimePicker.superclass.constructor.call(this,config);
        this.size = 1;
    },
    initFormat : function(){
        this.format=this.format||$S.defaultDateTimeFormat;
    },
    initFooter : function(){
        this.hourSpan = this.popup.child("input[stype=field.hour]");
        this.minuteSpan = this.popup.child("input[stype=field.minute]");
        this.secondSpan = this.popup.child("input[stype=field.second]");
    },
    processListener : function(ou){
        $S.DateTimePicker.superclass.processListener.call(this,ou);
        this.hourSpan[ou]("focus", this.onDateFocus, this);
        this.hourSpan[ou]("blur", this.onTimeInputBlur, this);
        this.hourSpan[ou]("keydown", this.onDateKeyDown, this);
        this.hourSpan[ou]("keyup", this.onDateKeyUp, this);
        this.minuteSpan[ou]("focus", this.onDateFocus, this);
        this.minuteSpan[ou]("blur", this.onTimeInputBlur, this);
        this.minuteSpan[ou]("keydown", this.onDateKeyDown, this);
        this.minuteSpan[ou]("keyup", this.onDateKeyUp, this);
        this.secondSpan[ou]("focus", this.onDateFocus, this);
        this.secondSpan[ou]("blur", this.onTimeInputBlur, this);
        this.secondSpan[ou]("keydown", this.onDateKeyDown, this);
        this.secondSpan[ou]("keyup", this.onDateKeyUp, this);
    },
    onDateKeyDown : function(e) {
        var c = e.keyCode, el = e.target;
        if (c == 13) {
            el.blur();
        } else if (c == 27) {
            el.value = el.oldValue || "";
            el.blur();
        } else if (c != 8 && c!=9 && c!=37 && c!=39 && c != 46 && (c < 48 || c > 57 || e.shiftKey)) {
            e.stopEvent();
            return;
        }
    },
    onDateKeyUp : function(e){
        var c = e.keyCode, el = e.target;
        if (c != 8 && c!=9 && c!=37 && c!=39 && c != 46 && (c < 48 || c > 57 || e.shiftKey)) {
            e.stopEvent();
            return;
        } else if(this.value&&this.value instanceof Date){
            var date=new Date(this.value.getTime());
            this.processDate(date);
            this.setValue(date);
            this.fireEvent('select',this, date);
        }
    },
    onDateFocus : function(e) {
        Ext.fly(e.target.parentNode).addClass("s_dateField-input-focus");
        e.target.select();
    },
    onTimeInputBlur : function(e) {
        var el = e.target;
        Ext.fly(el.parentNode).removeClass("s_dateField-input-focus");
        if(!el.value.match(/^[0-9]*$/))el.value=el.oldValue||"";
        else this.predraw(new Date(this.dateFields[0].year,this.dateFields[0].month - 1, 1,this.hourSpan.dom.value,this.minuteSpan.dom.value,this.secondSpan.dom.value));
    },
    predraw : function(date){
        $S.DateTimePicker.superclass.predraw.call(this,date);
        this.hourSpan.dom.oldValue = this.hourSpan.dom.value = $S.dateFormat.pad(date.getHours());
        this.minuteSpan.dom.oldValue = this.minuteSpan.dom.value = $S.dateFormat.pad(date.getMinutes());
        this.secondSpan.dom.oldValue = this.secondSpan.dom.value = $S.dateFormat.pad(date.getSeconds());
    },
    processDate : function(d){
        if(d){
            d.setHours((el=this.hourSpan.dom).value.match(/^[0-9]*$/)?el.value:el.oldValue);
            d.setMinutes((el=this.minuteSpan.dom).value.match(/^[0-9]*$/)?el.value:el.oldValue);
            d.setSeconds((el=this.secondSpan.dom).value.match(/^[0-9]*$/)?el.value:el.oldValue);
        }
        wrapDate(d)
    },
    wrapDate : function(d){
        d.xtype = 'timestamp';
    }
});